package com.inspur.edp.bef.bizentity.json.operation;

import com.fasterxml.jackson.core.JsonGenerator;
import com.inspur.edp.bef.bizentity.common.BizEntityJsonConst;
import com.inspur.edp.bef.bizentity.operation.BizActionBase;
import com.inspur.edp.bef.bizentity.operation.BizOperation;
import com.inspur.edp.bef.bizentity.operation.componentbase.BizParameter;
import com.inspur.edp.bef.bizentity.operation.componentinterface.IBizParameter;
import com.inspur.edp.cef.designtime.api.json.SerializerUtils;
import lombok.var;

public abstract class BizActionBaseSerializer extends BizOperationSerializer<BizActionBase> {

    public BizActionBaseSerializer(){}
    public BizActionBaseSerializer(boolean full){
        super(full);
        isFull = full;
    }
    @Override
    protected void writeExtendOperationBaseProperty(JsonGenerator writer, BizOperation op) {
        BizActionBase action = (BizActionBase) op;
        writeExtendActionBaseProperty(writer, action);
    }

    @Override
    protected void writeExtendOperationSelfProperty(JsonGenerator writer, BizOperation op) {
        BizActionBase action = (BizActionBase) op;
//        SerializerUtils.writePropertyValue(writer, BizEntityJsonConst.CurentAuthType, action.getCurentAuthType());
        if(isFull){
            writeOpIdList(writer, action);
        }
        writeParameters(writer, action);
        WriteReturnValue(writer, action);
        WriteExtendActionSelfProperty(writer, action);
    }

    private void writeOpIdList(JsonGenerator writer, BizActionBase action) {
        SerializerUtils.writePropertyName(writer, BizEntityJsonConst.OpIdList);
        //[
        SerializerUtils.WriteStartArray(writer);
        if (action.getOpIdList().size() > 0) {
            for (String item : action.getOpIdList()) {
                SerializerUtils.writePropertyValue_String(writer, item);
            }
        }
        //]
        SerializerUtils.WriteEndArray(writer);
    }

    private void writeParameters(JsonGenerator writer, BizActionBase action) {
        if(isFull||(action.getParameters()!=null&&action.getParameters().getCount()>0)) {
            SerializerUtils.writePropertyName(writer, BizEntityJsonConst.Parameters);
            //[
            SerializerUtils.WriteStartArray(writer);
            if(action.getParameters()!=null&&action.getParameters().getCount()>0){
                for (Object item : action.getParameters()) {
                getParaConvertor().serialize((BizParameter) item, writer, null);
            }
            }
            //]
            SerializerUtils.WriteEndArray(writer);
        }
    }

    private void WriteReturnValue(JsonGenerator writer, BizActionBase action)
    {
        if (action != null && action.getReturnValue() != null)
        {
            SerializerUtils.writePropertyName(writer, BizEntityJsonConst.ReturnValue);
            getParaConvertor().serialize(action.getReturnValue(),writer, null);
        }
    }


    protected abstract BizParameterSerializer getParaConvertor();

    protected abstract void writeExtendActionBaseProperty(JsonGenerator writer, BizActionBase action);

    protected abstract void WriteExtendActionSelfProperty(JsonGenerator writer, BizActionBase action);
}
